package com.mls.fivedistrict.server.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.dao.OrePoolMapper;
import com.mls.fivedistrict.enums.ResponseCode;
import com.mls.fivedistrict.pojo.*;
import com.mls.fivedistrict.server.IntegralService;
import com.mls.fivedistrict.server.OrePoolService;
import com.mls.fivedistrict.server.ProductPlanService;
import com.mls.fivedistrict.util.TimeConstants;
import com.mls.fivedistrict.util.TimeUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import static java.math.BigDecimal.ROUND_DOWN;

/**
 * Created by chenxiuxiang on 2018/4/1.
 */
@Service("orePoolService")
public class OrePoolServiceImpl implements OrePoolService {
    @Resource(name = "orePoolMapper")
    OrePoolMapper orePoolMapper;

    @Resource(name = "integralService")
    IntegralService integralService;

    @Resource(name = "productPlanService")
    ProductPlanService productPlanService;

    @Transactional
    @Override
    public ServiceResponse addOrePool(OrePool orePool) {
        orePool.setResidue(orePool.getTotal());
        Date endDate = TimeUtils.millis2Date((orePool.getEndDate().getTime() + (24 * 60 * 60 * 1000 - 1)));
        orePool.setEndDate(endDate);
        int row = orePoolMapper.insertSelective(orePool);
        if (row > 0) {
            ServiceResponse integralServiceIntegral = integralService.getIntegral(orePool.getIntrgralId());
            if (!integralServiceIntegral.getState().equals(ResponseCode.SUCCESS.getCode())) {
                return ServiceResponse.createErrorResponse("积分不存在");
            }
            Integral integral = (Integral) integralServiceIntegral.getData();
            //产生计划
            //1. 计算日期之间差值
            long days = TimeUtils.getTimeSpan(orePool.getStartDate(), orePool.getEndDate(), TimeConstants.DAY);
            days = days + 1;
            //每天生产多少
            BigDecimal divide = orePool.getTotal().divide(new BigDecimal(days), 8, ROUND_DOWN);
            //2.创建【days】条计划
            Date lastDate = orePool.getStartDate();
//            lastDate = TimeUtils.date2D(lastDate);
            for (int i = 0; i < days; i++) {
                ProductPlan productPlan = new ProductPlan();
                productPlan.setTotal(divide);
                productPlan.setDate(lastDate);
                productPlan.setFrequency(Long.valueOf(integral.getFrequencyId()));
                productPlan.setPoolId(orePool.getId());
                productPlan.setIntegtalId(orePool.getIntrgralId());
                productPlan.setResidue(divide);
                productPlanService.addProductPlan(productPlan);
                lastDate = TimeUtils.getAfterDays(lastDate);
            }
            return ServiceResponse.createSuccessResponse(orePool.getId());

        }
        return ServiceResponse.createErrorResponse("创建失败");
    }


    @Override
    public int editOrePool(OrePool orePool) {
        return orePoolMapper.updateByPrimaryKeySelective(orePool);
    }

    @Override
    public ServiceResponse getOrePoolList(PageFilter pageFilter) {
        OrePoolExample orePoolExample = new OrePoolExample();
        OrePoolExample.Criteria criteria = orePoolExample.createCriteria();
        Optional.ofNullable(pageFilter.getFilterMap().get("intrgralId")).map(intrgralId -> criteria.andIntrgralIdEqualTo(Long.valueOf(intrgralId)));
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List<OrePool> orePools = orePoolMapper.selectByExample(orePoolExample);
        PageInfo pageInfo = new PageInfo(orePools, 5);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }

    @Override
    public OrePool getOrePoolDetail(Long orePoolId) {
        return orePoolMapper.selectByPrimaryKey(orePoolId);
    }

    @Override
    public ServiceResponse deleteOrePool(Long orePoolId) {
        return null;
    }

    @Override
    public BigDecimal getAllPlanTotalByOrePoolId(Long id) {
        BigDecimal total = orePoolMapper.getProductPlanTotalByOreId(id);
        return total;
    }

}
